/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package unc.pds.facets;

import java.util.ArrayList;
import unc.pds.auth.IPDSSecurity;
import unc.pds.data.Consts;
import unc.pds.filemodel.AlbumRemote;
import unc.pds.filemodel.FileRemote;
import unc.pds.filemodel.FolderRemote;
import unc.pds.filemodel.PhotoRemote;
import unc.pds.model.DiaryRemote;
import unc.pds.model.GroupRemote;
import unc.pds.model.Model;
import unc.pds.model.Postable;
import unc.pds.model.Subject;
import unc.pds.model.UserRemote;
import unc.pds.service.ServiceBeanRemote;

/**
 *
 * @author Михаил
 */
public class FacetManager {

    public static ArrayList getFacetList(String id, String view, ServiceBeanRemote service, IPDSSecurity security) throws Exception {
        ArrayList result = new ArrayList();
        Model object = (Model) service.getByKey(Long.decode(id));
        Postable sub = service.getUser();
        if (object instanceof UserRemote) {
            UserRemote user = (UserRemote) object;
            if (view.equals("posts")) {         
                if (security.getCrPermission(user.getKey(), Consts.OBJECT_TYPE__DIARY)) {
                    result.add(new AddPostFacet(user));
                }
                if (security.getPermission(user.getKey(), Consts.ATTR__USER_DIARY, "read")) {
                    result.add(new PostsFacet(user, security));
                }
            } else if (view.equals("friends")) {
                if (security.getPermission(user.getKey(), Consts.ATTR__USER_NEWFRIEND, "read")) {
                    result.add(new NewFriendsFacet(user));
                    result.add(new FriendsFacet(user, true));
                } else {
                    result.add(new FriendsFacet(user, false));
                }
            } else if (view.equals("groups")) {
                if (user.getKey() == service.getUser().getKey()) {
                    result.add(new GroupsFacet(user, true, security));
                    result.add(new CreateGroupFacet(user));
                } else {
                    result.add(new GroupsFacet(user, false, security));
                }
            } else if (view.equals("folders")) {
                if (security.getCrPermission(user.getKey(), Consts.OBJECT_TYPE__FOLDER)) {
                    result.add(new AddFolderFacet(user));
                    result.add(new FoldersFacet(user, security));
                } else {
                    result.add(new FoldersFacet(user, security));
                }
            } else if (view.equals("albums")) {
                if (security.getCrPermission(user.getKey(), Consts.OBJECT_TYPE__ALBUM)) {
                    result.add(new AddAlbumFacet(user));
                    result.add(new AlbumsFacet(user, security));
                } else {
                    result.add(new AlbumsFacet(user, security));
                }
            } else if (view.equals("news")) {
                if (user.getKey()==service.getUser().getKey()) {
                    result.add(new NewsFacet(user,service));
                }
            }
            sub = user;
        } else if (object instanceof GroupRemote) {
            GroupRemote group = (GroupRemote)object;
            if (view.equals("posts")) {
                if (security.getCrPermission(group.getKey(), Consts.OBJECT_TYPE__DIARY)) {
                    result.add(new AddPostFacet(group));
                }
                if (security.getPermission(group.getKey(), Consts.ATTR__USER_DIARY, "read")) {
                    result.add(new PostsFacet(group, security));
                }
            } else if (view.equals("members")) {
                if (security.getPermission(group.getKey(), Consts.ATTR__GROUP_NEWMEMBER, "read")) {
                    result.add(new NewMembersFacet(group));
                    result.add(new MembersFacet(group, security));
                } else {
                    result.add(new MembersFacet(group, security));
                }
            } else if (view.equals("folders")) {
                if (security.getCrPermission(group.getKey(), Consts.OBJECT_TYPE__FOLDER)) {
                    result.add(new AddFolderFacet(group));
                    result.add(new FoldersFacet(group, security));
                } else {
                    result.add(new FoldersFacet(group, security));
                }
            } else if (view.equals("albums")) {
                if (security.getCrPermission(group.getKey(), Consts.OBJECT_TYPE__ALBUM)) {
                    result.add(new AddAlbumFacet(group));
                    result.add(new AlbumsFacet(group, security));
                } else {
                    result.add(new AlbumsFacet(group, security));
                }
            }
            sub = group;
            
        } else if (object instanceof DiaryRemote) {
            DiaryRemote diary = (DiaryRemote) object;
            result.add(new PostFacet(diary));
            result.add(new CommentsFacet(diary,security));
            sub = diary.getContext();
        } else if (object instanceof FolderRemote) {
            FolderRemote folder = (FolderRemote) object;
            if ((service.getUser().getKey() == folder.getAuthor().getKey())) {
                result.add(new AddFolderFacet(folder));
                result.add(new AddFileFacet(folder));
                result.add(new FolderFacet(folder, true));
            } else {
                result.add(new FolderFacet(folder, false));
            }
            sub = folder.getContext();
        } else if (object instanceof FileRemote) {
            FileRemote file = (FileRemote) object;
            if ((service.getUser().getKey() == file.getAuthor().getKey())) {
                result.add(new FileFacet(file, true));
            } else {
                result.add(new FileFacet(file, false));
            }
            result.add(new CommentsFacet(file,security));
            sub = file.getContext();
        } else if (object instanceof AlbumRemote) {
            AlbumRemote album = (AlbumRemote) object;
            if ((service.getUser().getKey() == album.getAuthor().getKey())) {
                result.add(new AddPhotoFacet(album));
                result.add(new AlbumFacet(album, true));
            } else {
                result.add(new AlbumFacet(album, false));
            }
            sub = album.getContext();
        } else if (object instanceof PhotoRemote) {
            PhotoRemote photo = (PhotoRemote) object;
            if ((service.getUser().getKey() == photo.getAuthor().getKey())) {
                result.add(new PhotoFacet(photo, true));
            } else {
                result.add(new PhotoFacet(photo, false));
            }
            result.add(new CommentsFacet(photo, security));
            sub = photo.getContext();
        }
        ArrayList result1 = new ArrayList();
        if (sub instanceof UserRemote) {
            result1.add(new UserNameFacet((UserRemote)sub));
            if (sub.getKey() != service.getUser().getKey()) {
                result.add(new AddFriendFacet((UserRemote)sub, service.getUser()));
            }
            if (security.getPermission(sub.getKey(), Consts.ATTR__USER_FRIEND, "read")) {
                result1.add(new ShortFriendsFacet((UserRemote) sub));
            }
            if (security.getPermission(sub.getKey(), Consts.ATTR__USER_MEMBER_OF, "read")) {
                result1.add(new ShortGroupsFacet((UserRemote) sub));
            }
            if (sub.getKey()==service.getUser().getKey()) {
                result.add(new ShortNewsFacet((UserRemote)sub ,service));
            }
        } else if (sub instanceof GroupRemote) {
            result1.add(new GroupNameFacet((GroupRemote)sub));
            result1.add(new AddGroupFacet((GroupRemote)sub, service.getUser()));
            if (security.getPermission(sub.getKey(), Consts.ATTR__GROUP_MEMBER, "read")) {
                result1.add(new ShortMembersFacet((GroupRemote) sub));
            }
        }
        if (security.getPermission(sub.getKey(), Consts.OBJECT_TYPE__ALBUM, "read")) {
            result1.add(new ShortAlbumsFacet(sub));
        }
        if (security.getPermission(sub.getKey(), Consts.OBJECT_TYPE__FOLDER, "read")) {
            result1.add(new ShortFilesFacet(sub));
        }
        result1.addAll(result);
        return result1;
    }
}
